import re
from bs4 import BeautifulSoup
from lxml import etree
from lxml import html as lhtml




file_path = "D:\\Crawl_Results\\htmls\\subject_20495023.html"



def get_title_by_regex(html):
    title_regex = '<span property="v:itemreviewed">(.*?)</span>'
    html_string = html.decode("utf-8")
    regex_do = re.compile(title_regex,re.IGNORECASE)
    tags = regex_do.findall(html_string)
    print("电影标题==>{}".format(tags[0]))

def get_title_by_beautiful_soup(html):
    html_string = html.decode("utf-8")
    soup = BeautifulSoup(html_string,"html.parser")
    father_div = soup.find('div',id="content")
    if father_div:
        info_span = father_div.find(attrs={'property':'v:itemreviewed'})
        if info_span:
            movie_title = info_span.text
            print("电影的标题是==>{}".format(movie_title))

def get_title_by_lxml(html):
    html_string = html.decode("utf-8")
    root = lhtml.fromstring(html_string)
    try:
        div_content = root.cssselect("div#content")[0]
        span_info = div_content.cssselect('span[property="v:itemreviewed"]')[0]
        title_text = span_info.text_content()
        print("电影的标题是==>{}".format(title_text))
    except IndexError:
        print("未找到指定的元素")
    except Exception as e:
        print(f"处理过程中发生错误: {e}")


with open(file_path,'rb') as html_file:
    html = html_file.read()

#get_title_by_regex(html)
#get_title_by_beautiful_soup(html)
get_title_by_lxml(html)
